/**
 *
 */
package org.etlt.expression.function;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;


/**
 * system default functions for String
 *
 * @version 2.0
 */
public class StringFunctions {

    @FunctionEnabled(value = "lower", help = "return lower of input string")
    public String lower(String arg) {
        return arg.toLowerCase();
    }


    @FunctionEnabled(value = "minString", help = "return lower of input string, or return a string which is not empty")
    public String minimum(String s1, String s2) {
        if (StringUtils.isEmpty(s1))
            return s2;
        if (StringUtils.isEmpty(s2))
            return s1;
        return s1.compareTo(s2) > 0 ? s2 : s1;
    }

    @FunctionEnabled(value = "maxString", help = "return maximum of input string, null is minimum")
    public String maximum(List<String> ss) {
        if (ss == null || ss.size() < 2)
            throw new IllegalArgumentException("this function need 2 or more arguments.");
        String result = ss.get(0);
        for (String s : ss) {
            if (result == null ||(s != null && s.compareTo(result) > 0)) {
                result = s;
            }
        }
        return result;
    }

    @FunctionEnabled("upper")
    public String upper(String arg) {
        return arg.toUpperCase();
    }

    /**
     * concat all strings
     *
     * @return
     */
    @FunctionEnabled("CONCAT")
    public String concat(Object... args) {
        if (args.length < 2) {
            throw new IllegalArgumentException("concat need at least 2 arguments.");
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (Object object : args) {
            stringBuilder.append(object);
        }
        return stringBuilder.toString();
    }

    @FunctionEnabled("subString")
    public String subString(Object... args) {
        String s1 = args[0].toString();
        int from = Integer.parseInt(args[1].toString());
        if (args.length > 2) {
            int to = Integer.parseInt(args[2].toString());
            return s1.substring(from, to);
        } else
            return s1.substring(from);
    }

    /**
     * String prefix comparison
     *
     * @param str1
     * @param str2
     * @return
     */
    @FunctionEnabled(value = "STARTSWITH", help = "return true: first string starts with second string.")
    public boolean startsWith(String str1, String str2) {
        return str1.startsWith(str2);
    }

    /**
     * String suffix comparison
     *
     * @param str1
     * @param str2
     * @return
     */
    @FunctionEnabled(value = "ENDSWITH", help = "return true: first string ends with second string.")
    public boolean endsWith(String str1, String str2) {
        return str1.endsWith(str2);
    }

    @FunctionEnabled(value = "LENGTH", help = "calculate the length of a String, 0 for null")
    public int getLength(String str) {
        return StringUtils.isBlank(str) ? 0 : str.length();
    }

    @FunctionEnabled(value = "is_empty", help = "check if an object is null or a string is blank")
    public boolean isEmpty(Object object) {
        if (object == null)
            return true;
        return StringUtils.isBlank(String.valueOf(object));
    }
}
